// 2、无头双向链表实现
public class LinkedList {
    static class ListNode{
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode last;
    //头插法
    public void addFirst(int data){
        ListNode cur = new ListNode(data);
        if(head == null){
            head = last = cur;
            return;
        }
        cur.next = head;
        head.prev = cur;
        head = cur;
    }
    //尾插法
    public void addLast(int data){
        ListNode cur = new ListNode(data);
        if(last == null){
            head = last = cur;
            return;
        }
        last.next = cur;
        cur.prev = last;
        last = cur;
    }
    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data) throws IndexIllegalException{
        try{
            if(index < 0 || index > size()){
                throw new IndexIllegalException("插入位置不合法");
            } else if (index == 0) {
                addFirst(data);
                return true;
            } else if (index == size()) {
                addLast(data);
                return true;
            }else {
                ListNode cur = head;
                ListNode newNode = new ListNode(data);
                for (int i = 0; i < index; i++) {
                    cur = cur.next;
                }
                newNode.prev = cur.prev;
                cur.prev = newNode;
                newNode.next = cur;
                return true;
            }

        }catch (IndexIllegalException e){
            e.printStackTrace();
            return false;
        }
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode cur = head;
        if(head.val == key){
            head.next.prev = null;
            head = head.next;
            return;
        } else {
            while(cur.next != null){
                if(cur.val == key){
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                    return;
                }
                cur = cur.next;
            }
            if (last.val == key) {
                last.prev.next = null;
                last = last.prev;
                return;
            }
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode cur = head.next;
        while(cur.next != null){
            if(cur.val == key){
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
            }
            cur = cur.next;
        }
        if(head.val == key){
            head.next.prev = null;
            head = head.next;
        }
        if (last.val == key) {
            last.prev.next = null;
            last = last.prev;
        }
    }
    //得到单链表的长度
    public int size(){
        ListNode cur = head;
        int size = 0;
        while(cur != null){
            size++;
            cur = cur.next;
        }
        return size;
    }
    public void display(){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
    }
    public void clear(){
        ListNode cur = head;
        while(cur != null){
            ListNode curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        head = null;
        last = null;
    }
}
